package com.lhh.userservice.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhh.springcommon.annotation.WebLog;
import com.lhh.springcommon.entity.Follow;
import com.lhh.springcommon.entity.vo.UserVO;
import com.lhh.springcommon.mapper.FollowMapper;
import com.lhh.springcommon.service.IFollowService;
import com.lhh.springcommon.utils.PageQuery;
import com.lhh.springcommon.interceptor.JwtInterceptor;
import com.lhh.userservice.service.UserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 李厚航
 * @date 2025-03-01 22:02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserFollowServiceImpl implements UserFollowService {

    private final IFollowService followService;

    private final FollowMapper followMapper;

    @Autowired
    public UserFollowServiceImpl(IFollowService followService, FollowMapper followMapper) {
        this.followService = followService;
        this.followMapper = followMapper;
    }


    @Override
    @WebLog
    public Long countFollow(Integer uid,Integer fid) {
        return followService.count(new LambdaQueryWrapper<Follow>()
                .eq(uid!=null,Follow::getUid, uid)
                .eq(fid!=null,Follow::getFid,fid));
    }

    @Override
    @WebLog
    public Long countFans() {
        Integer userId = JwtInterceptor.getUserIdFromThreadLocal();
        return followService.count(new LambdaQueryWrapper<Follow>().eq(Follow::getFid, userId));
    }

    @Override
    @WebLog
    @Cacheable(value = "queryPageFans",key = "#page + ':' + #pageSize + ':' + #uid + ':' + #fid")
    public Page<UserVO> queryPageFans(Integer page,Integer pageSize,Integer uid,Integer fid) {
        Follow follow = new Follow();
        follow.setUid(uid);
        follow.setFid(fid);
        return followMapper.queryPageFansVO(follow, new Page<>(page, pageSize));
    }

    @Override
    @WebLog
    @Caching(
            evict = {
                    @CacheEvict(value = "countFollow",key = "T(com.lhh.springcommon.interceptor.JwtInterceptor).userIdFromThreadLocal"),
                    @CacheEvict(value = "countFans",key = "T(com.lhh.springcommon.interceptor.JwtInterceptor).userIdFromThreadLocal"),
                    @CacheEvict(value = "queryPageFans",allEntries = true),
                    @CacheEvict(value = "queryPageFollow",allEntries = true)
            }
    )
    public Boolean addOrDeleteFollow(Follow follow) {
        LambdaQueryWrapper<Follow> eq = new LambdaQueryWrapper<Follow>()
                .eq(follow.getUid() != null, Follow::getUid, follow.getUid())
                .eq(follow.getFid() != null, Follow::getFid, follow.getFid())
                .eq(follow.getId() != null, Follow::getId, follow.getId());
        Follow one = followService.getOne(eq);
        if (one == null) {
            //关注
            return followService.save(follow);
        }
        return followService.remove(eq);
    }

    @Override
    @WebLog
    @Cacheable(value = "queryPageFollow",key = "#page + ':' + #pageSize + ':' + #uid + ':' + #fid")
    public Page<UserVO> queryPageFollow(Integer page,Integer pageSize,Integer uid,Integer fid) {
        Follow follow = new Follow();
        follow.setUid(uid);
        follow.setFid(fid);
        return followMapper.queryPageFollowVO(follow, new Page<>(page, pageSize));
    }

    @Override
    @WebLog
    public Boolean isFollow(Integer uid,Integer fid) {
        return followService.getOne(new LambdaQueryWrapper<Follow>()
                .eq(Follow::getUid, uid)
                .eq(Follow::getFid, fid)) != null;
    }
}
